﻿using Framework.EncryptionService.Attributes;
using Framework.EncryptionService.Services;

namespace Framework.EncryptionService
{
    /// <summary>
    /// 返回值加密
    /// </summary>
    public class ReturnValueEncryption
    {
        /// <summary>
        /// 返回值加密
        /// </summary>
        /// <param name="responseBodyContent">返回值熙熙</param>
        /// <param name="str_aesSecret">AES秘钥</param>
        /// <param name="attribute">不加密特性</param>
        /// <returns>处理过的返回值</returns>
        public static string ReturnValueEncryptionHelps(string responseBodyContent,string str_aesSecret, NonEncryptedAttribute attribute)
        {
            string processedContent ;
            if (attribute == null || attribute.EncryptionTypes == EncryptionType.Response || attribute.EncryptionTypes == EncryptionType.Full)//需要对返回值进行加密
            {
                var data = str_aesSecret.ConvertToObject<EncryptionServiceHelpsEntity>();
                if (!string.IsNullOrWhiteSpace(str_aesSecret) || data == null)
                {
                    EncryptionAESServiceHelps aesService = new EncryptionAESServiceHelps(data ?? new EncryptionServiceHelpsEntity());
                    try
                    {
                        processedContent = aesService.Encrypt(responseBodyContent).ToBase64String();
                    }
                    catch (Exception e)
                    {
                        processedContent = $"返回值加密失败，异常原因：({e.Message})";
                    }
                }
                else
                {
                    processedContent = "未获取到前端加密秘钥";
                }
            }
            else//如果当前接口实现了不加密特性，则不对返回值进行加密
            {
                processedContent = responseBodyContent;
            }
            return processedContent;
        }
    }
}
